/* Copyright (C) 2011 Muhammad Edwin & Natalino Nugeraha
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.

3. The name "FastStringUtility" must not be used to endorse or promote products
derived from this software without prior written permission.  For
written permission.

4. Products derived from this software may not be called "FastStringUtility", nor
may "FastStringUtility" appear in their name, without prior written permission
from the FastStringUtility Project Management.

In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
FastStringUtil Project (http://www.baculsoft.com/)."

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

This software consists of voluntary contributions made by many
individuals on behalf of the FastStringUtil Project and was originally
created by Natalino Nugeraha Putrama <nugie@baculsoft.com> and
Muhammad Edwin <edwinkun@gmail.com>.  For more information
on the FastStringUtil Project, please see <http://www.baculsoft.com/>.
 */

package net.sf.commonstringutil;


/**
 * 
 * @author Natalino Nugeraha
 * @version 1.0.0
 * @category internal utility
 */

final class FRandom {
	
    private static final int N = 624;
    private static final int N_DELTA=N-1;
    private static final int M = 397;
    private static final int M_DELTA=M-1;
    private static final int LOOP=N-M;    
    private static final int M_DELTA_N=M-N;
    
    private static final int LOWER_MASK = 0x7fffffff;
    private static final int UPPER_MASK = 0x80000000;
    private static final int MASK_A = 0x9d2c5680;
    private static final int MASK_B = 0xefc60000;

    private static final int MTRX = 0x9908b0df;    
    private transient final int mt[];
    private transient int mti; 
    private transient int mag[];
    private transient double nextGaussian;
    private transient boolean haveNextGaussian;

    FRandom(){
        int _seed = 4357;
        this.haveNextGaussian = false;
        this.mt = new int[N];
        for (int i = N_DELTA; i >=0; i--){
            this.mt[i] =( _seed & 0xffff0000 ) | (int)System.nanoTime();
            _seed = 69069 * (_seed + 1)+ ((int)System.nanoTime()+(int)System.nanoTime());
            this.mt[i] |= (_seed & 0xffff0000) >>> 16;
            _seed = 69069 * _seed + 1;
        }
        this.mti = N;    
        this.mag = new int[2];
        this.mag[0] = 0x0;
        this.mag[1] = MTRX;        
    }

    /**
     * 
     * @param seed
     */
    FRandom(final long seed){
        this.haveNextGaussian = false;
        int _seed=(int)seed;
        this.mt = new int[N];

        for (int i = N_DELTA; i >= 0; i--){
            this.mt[i] =( _seed & 0xffff0000 ) | (int)System.nanoTime();
            _seed = 69069 * (_seed + 1)+ ((int)System.nanoTime()+(int)System.nanoTime());
            this.mt[i] |= (_seed & 0xffff0000) >>> 16;
            _seed = 69069 * _seed + 1;
        }
        this.mti = N;
        this.mag = new int[2];
        this.mag[0] = 0x0;
        this.mag[1] = MTRX;                
    }

    
    /**
     * 
     * @return next random integer
     */
    final int nextInt(){
        int y;
        if (this.mti >= N) {
            int cnt;           
            for (cnt = 0; cnt <LOOP; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
            this.mti = 0;
        }
        y = this.mt[this.mti++];
        y ^= y >>> 11;
        y ^= (y << 7) & MASK_A;
        y ^= (y << 15) & MASK_B;
        y ^= (y >>> 18);
        return y;
    }

    
    /**
     * 
     * @return next random short 
     */
    final short nextShort(){
        int y;
        if (this.mti >= N){
            int cnt;
            for (cnt = 0; cnt < LOOP; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
            this.mti = 0;
        }

        y = this.mt[this.mti++];
        y ^= y >>> 11; 
        y ^= (y << 7) & MASK_A;
        y ^= (y << 15) & MASK_B; 
        y ^= (y >>> 18); 
        return (short) (y >>> 16);
    }

    
    /**
     * 
     * @return random char 
     */
    final char nextChar(){
        int y;
        if (this.mti >= N){
            int cnt;
            for (cnt = 0; cnt < LOOP; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
            this.mti = 0;
        }
        y = this.mt[this.mti++];
        y ^= y >>> 11; 
        y ^= (y << 7) & MASK_A; 
        y ^= (y << 15) & MASK_B; 
        y ^= (y >>> 18); 
        return (char) (y >>> 16);
    }

   
    /**
     * 
     * @return next random boolean
     */
    final boolean nextBoolean(){
        int y;
        if (this.mti >= N) {
            int cnt;
            for (cnt = 0; cnt < LOOP; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];

            this.mti = 0;
        }
        y = this.mt[this.mti++];
        y ^= y >>> 11; 
        y ^= (y << 7) & MASK_A; 
        y ^= (y << 15) & MASK_B;
        y ^= (y >>> 18); 
        return ((y >>> 31) != 0);
    }

    /**
     * @param probability between 0.0 and 1.0
     * @return the result of the coin flip
     */

    final boolean nextBoolean(final float probability){
        int y;
        if (probability < 0.0f || probability > 1.0f)
            throw new IllegalArgumentException("probability must be between 0.0 and 1.0 inclusive.");
        if (this.mti >= N){
            int cnt;
            for (cnt = 0; cnt < LOOP; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
            this.mti = 0;
        }
        y = this.mt[this.mti++];
        y ^= y >>> 11; 
        y ^= (y << 7) & MASK_A;
        y ^= (y << 15) & MASK_B; 
        y ^= (y >>> 18);
        return (y >>> 8) / ((float) (1 << 24)) < probability;
    }

    /**
     * 
     * @param probability between 0.0 and 1.0
     * @return the result of the coin flip
     */

    final boolean nextBoolean(final double probability){
        int y,z;
        if (probability < 0.0 || probability > 1.0)
            throw new IllegalArgumentException("probability must be between 0.0 and 1.0 inclusive."); 
        if (this.mti >= N){
            int cnt;
            for (cnt = 0; cnt <LOOP; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
            this.mti = 0;
        }

        y = this.mt[this.mti++];
        y ^= y >>> 11; 
        y ^= (y << 7) & MASK_A;
        y ^= (y << 15) & MASK_B;
        y ^= (y >>> 18);
        if (this.mti >= N) {
            int cnt;
            for (cnt = 0; cnt < LOOP; cnt++){
                z = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (z >>> 1) ^ this.mag[z & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                z = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (z >>> 1) ^ this.mag[z & 0x1];
            }
            z = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (z >>> 1) ^ this.mag[z & 0x1];
            this.mti = 0;
        }
        z = this.mt[this.mti++];
        z ^= z >>> 11;
        z ^= (z << 7) & MASK_A;
        z ^= (z << 15) & MASK_B;
        z ^= (z >>> 18); 
        return ((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53) < probability;
    }

    /**
     * @return the next random byte
     */
    final byte nextByte()
    {
        int y;
        if (this.mti >= N){
            int cnt;
            for (cnt = 0; cnt < LOOP; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];

            this.mti = 0;
        }
        y = this.mt[this.mti++];
        y ^= y >>> 11;
        y ^= (y << 7) & MASK_A;
        y ^= (y << 15) & MASK_B;
        y ^= (y >>> 18);
        return (byte) (y >>> 24);
    }

    /**
     * @param bytes the next bytes
     * @return the next random of byte
     */
    final void nextBytes(byte[] bytes){
        int y;
        for (int x = 0; x < bytes.length; x++){
            if (this.mti >= N) {
                int cnt;
                for (cnt = 0; cnt < LOOP; cnt++){
                    y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                    this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
                }
                for (; cnt < N_DELTA; cnt++){
                    y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                    this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
                }
                y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
                this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
                this.mti = 0;
            }
            y = this.mt[this.mti++];
            y ^= y >>> 11; 
            y ^= (y << 7) & MASK_A; 
            y ^= (y << 15) & MASK_B; 
            y ^= (y >>> 18);
            bytes[x] = (byte) (y >>> 24);
        }
    }

    /**
     * @return the next random of long
     */
    final long nextLong(){
        int y,z;
        if (this.mti >= N) {
            for (z = 0; z < LOOP; z++){
                y = (this.mt[z] & UPPER_MASK) | (this.mt[z + 1] & LOWER_MASK);
                this.mt[z] = this.mt[z + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; z < N_DELTA; z++){
                y = (this.mt[z] & UPPER_MASK) | (this.mt[z + 1] & LOWER_MASK);
                this.mt[z] = this.mt[z + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
            this.mti = 0;
        }
        y = this.mt[this.mti++];
        y ^= y >>> 11;
        y ^= (y << 7) & MASK_A;
        y ^= (y << 15) & MASK_B; 
        y ^= (y >>> 18);
        if (this.mti >= N){
            int cnt;
            for (cnt = 0; cnt <LOOP; cnt++){
                z = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (z >>> 1) ^ this.mag[z & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                z = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (z >>> 1) ^ this.mag[z & 0x1];
            }
            z = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (z >>> 1) ^ this.mag[z & 0x1];
            this.mti = 0;
        }

        z = this.mt[this.mti++];
        z ^= z >>> 11;
        z ^= (z << 7) & MASK_A;
        z ^= (z << 15) & MASK_B;
        z ^= (z >>> 18);
        return (((long) y) << 32) + z;
    }

    /**
     * @return the next random of double
     */
    final double nextDouble(){
        int y,z;
        if (this.mti >= N){
        	z=0;
            for (; z < LOOP; z++){
                y = (this.mt[z] & UPPER_MASK) | (this.mt[z + 1] & LOWER_MASK);
                this.mt[z] = this.mt[z + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; z < N_DELTA; z++){
                y = (this.mt[z] & UPPER_MASK) | (this.mt[z + 1] & LOWER_MASK);
                this.mt[z] = this.mt[z + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
            this.mti = 0;
        }

        y = this.mt[this.mti++];
        y ^= y >>> 11; 
        y ^= (y << 7) & MASK_A;
        y ^= (y << 15) & MASK_B; 
        y ^= (y >>> 18); 
        if (this.mti >= N) {
            int cnt=0;
            for (; cnt < LOOP; cnt++){
                z = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (z >>> 1) ^ this.mag[z & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                z = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (z >>> 1) ^ this.mag[z & 0x1];
            }
            z = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (z >>> 1) ^ this.mag[z & 0x1];
            this.mti = 0;
        }
        z = this.mt[this.mti++];
        z ^= z >>> 11; 
        z ^= (z << 7) & MASK_A;
        z ^= (z << 15) & MASK_B;
        z ^= (z >>> 18);
        return ((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53);
    }

    /**
     * 
     * @return next gaussian of double 
     */
    final double nextGaussian(){
        if (this.haveNextGaussian){
            this.haveNextGaussian = false;
            return this.nextGaussian;
        }
        double v1, v2, s;
        do{
            int y,z,a,b;
            if (this.mti >= N){
            	z=0;
                for (; z < LOOP; z++){
                    y = (this.mt[z] & UPPER_MASK) | (this.mt[z + 1] & LOWER_MASK);
                    this.mt[z] = this.mt[z + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
                }
                for (; z < N_DELTA; z++){
                    y = (this.mt[z] & UPPER_MASK) | (this.mt[z + 1] & LOWER_MASK);
                    this.mt[z] = this.mt[z + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
                }
                y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
                this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
                this.mti = 0;
            }
            y = this.mt[this.mti++];
            y ^= y >>> 11;
            y ^= (y << 7) & MASK_A;
            y ^= (y << 15) & MASK_B; 
            y ^= (y >>> 18); 
            if (this.mti >= N) {
                for (a = 0; a < LOOP; a++){
                    z = (this.mt[a] & UPPER_MASK) | (this.mt[a + 1] & LOWER_MASK);
                    this.mt[a] = this.mt[a + M] ^ (z >>> 1) ^ this.mag[z & 0x1];
                }
                for (; a < N_DELTA; a++){
                    z = (this.mt[a] & UPPER_MASK) | (this.mt[a + 1] & LOWER_MASK);
                    this.mt[a] = this.mt[a + (M_DELTA_N)] ^ (z >>> 1) ^ this.mag[z & 0x1];
                }
                z = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
                this.mt[N_DELTA] = this.mt[M_DELTA] ^ (z >>> 1) ^ this.mag[z & 0x1];
                this.mti = 0;
            }
            z = this.mt[this.mti++];
            z ^= z >>> 11;
            z ^= (z << 7) & MASK_A;
            z ^= (z << 15) & MASK_B; 
            z ^= (z >>> 18); 
            if (this.mti >= N) {
            	b=0;
                for (; b < LOOP; b++){
                    a = (this.mt[b] & UPPER_MASK) | (this.mt[b + 1] & LOWER_MASK);
                    this.mt[b] = this.mt[b + M] ^ (a >>> 1) ^ this.mag[a & 0x1];
                }
                for (; b < N_DELTA; b++){
                    a = (this.mt[b] & UPPER_MASK) | (this.mt[b + 1] & LOWER_MASK);
                    this.mt[b] = this.mt[b + (M_DELTA_N)] ^ (a >>> 1) ^ this.mag[a & 0x1];
                }
                a = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
                this.mt[N_DELTA] = this.mt[M_DELTA] ^ (a >>> 1) ^ this.mag[a & 0x1];
                this.mti = 0;
            }
            a = this.mt[this.mti++];
            a ^= a >>> 11;
            a ^= (a << 7) & MASK_A; 
            a ^= (a << 15) & MASK_B; 
            a ^= (a >>> 18);
            if (this.mti >= N){
                int cnt=0;
                for (; cnt < LOOP; cnt++){
                    b = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                    this.mt[cnt] = this.mt[cnt + M] ^ (b >>> 1) ^ this.mag[b & 0x1];
                }
                for (; cnt < N_DELTA; cnt++){
                    b = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                    this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (b >>> 1) ^ this.mag[b & 0x1];
                }
                b = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
                this.mt[N_DELTA] = this.mt[M_DELTA] ^ (b >>> 1) ^ this.mag[b & 0x1];
                this.mti = 0;
            }
            b = this.mt[this.mti++];
            b ^= b >>> 11; 
            b ^= (b << 7) & MASK_A; 
            b ^= (b << 15) & MASK_B;
            b ^= (b >>> 18); 
            v1 = 2 * (((((long) (y >>> 6)) << 27) + (z >>> 5)) / (double) (1L << 53)) - 1;
            v2 = 2 * (((((long) (a >>> 6)) << 27) + (b >>> 5)) / (double) (1L << 53)) - 1;
            s = v1 * v1 + v2 * v2;
        }while (s >= 1 || s == 0);
        double multiplier = Math.sqrt(-2 * Math.log(s) / s);
        this.nextGaussian = v2 * multiplier;
        this.haveNextGaussian = true;
        return v1 * multiplier;
    }

    
    /**
     * 
     * @return next random of float
     */
    final float nextFloat(){
        int y;
        if (this.mti >= N) {
            int cnt;
            for (cnt = 0; cnt < LOOP; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            for (; cnt < N_DELTA; cnt++){
                y = (this.mt[cnt] & UPPER_MASK) | (this.mt[cnt + 1] & LOWER_MASK);
                this.mt[cnt] = this.mt[cnt + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
            }
            y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
            this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
            this.mti = 0;
        }
        y = this.mt[this.mti++];
        y ^= y >>> 11; 
        y ^= (y << 7) & MASK_A; 
        y ^= (y << 15) & MASK_B;
        y ^= (y >>> 18); 
        return (y >>> 8) / ((float) (1 << 24));
    }

    
    /**
     * 
     * @param n must greather than 0
     * @return random of int 
     */
    final int nextInt(final int n){
        if (n <= 0)throw new IllegalArgumentException("n must be positive");
        int y,bits;       
        if ((n & -n) == n){
            if (this.mti >= N){
            	bits=0;
                for (; bits < LOOP; bits++){
                    y = (this.mt[bits] & UPPER_MASK) | (this.mt[bits + 1] & LOWER_MASK);
                    this.mt[bits] = this.mt[bits + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
                }
                for (; bits < N_DELTA; bits++){
                    y = (this.mt[bits] & UPPER_MASK) | (this.mt[bits + 1] & LOWER_MASK);
                    this.mt[bits] = this.mt[bits + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
                }
                y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
                this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
                this.mti = 0;
            }
            y = this.mt[this.mti++];
            y ^= y >>> 11; 
            y ^= (y << 7) & MASK_A; 
            y ^= (y << 15) & MASK_B; 
            y ^= (y >>> 18);
            return (int) ((n * (long) (y >>> 1)) >> 31);
        }
        int val;
        do{
            if (this.mti >= N){
                for (bits = 0; bits < LOOP; bits++){
                    y = (this.mt[bits] & UPPER_MASK) | (this.mt[bits + 1] & LOWER_MASK);
                    this.mt[bits] = this.mt[bits + M] ^ (y >>> 1) ^ this.mag[y & 0x1];
                }
                for (; bits < N_DELTA; bits++){
                    y = (this.mt[bits] & UPPER_MASK) | (this.mt[bits + 1] & LOWER_MASK);
                    this.mt[bits] = this.mt[bits + (M_DELTA_N)] ^ (y >>> 1) ^ this.mag[y & 0x1];
                }
                y = (this.mt[N_DELTA] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
                this.mt[N_DELTA] = this.mt[M_DELTA] ^ (y >>> 1) ^ this.mag[y & 0x1];
                this.mti = 0;
            }
            y = this.mt[this.mti++];
            y ^= y >>> 11; 
            y ^= (y << 7) & MASK_A;
            y ^= (y << 15) & MASK_B; 
            y ^= (y >>> 18);
            bits = (y >>> 1);
            val = bits % n;
        }while (bits - val + (N_DELTA) < 0);
        return val;
    }
}